home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / JavaIDL-EA-win32.EXE / JavaIDL / examples / portfolio / PortfolioApplet.java < prev    next >
Encoding:
Java Source  |  1997-03-05  |  12.6 KB  |  415 lines

  1. /*
  2.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  5.  * modify and redistribute this software in source and binary code form,
  6.  * provided that i) this copyright notice and license appear on all copies of
  7.  * the software; and ii) Licensee does not utilize the software in a manner
  8.  * which is disparaging to Sun.
  9.  *
  10.  * This software is provided "AS IS," without a warranty of any kind. ALL
  11.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  12.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  13.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  14.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  15.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  16.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  17.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  18.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  19.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  20.  * POSSIBILITY OF SUCH DAMAGES.
  21.  *
  22.  * This software is not designed or intended for use in on-line control of
  23.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  24.  * the design, construction, operation or maintenance of any nuclear
  25.  * facility. Licensee represents and warrants that it will not use or
  26.  * redistribute the Software for such purposes.
  27.  */
  28.  
  29.  
  30. import java.lang.*;
  31. import java.awt.*;
  32. import java.applet.*;
  33.  
  34. import java.io.*;
  35.  
  36. import org.omg.CosNaming.*;
  37. import org.omg.CosNaming.NamingContextPackage.*;
  38. import org.omg.CORBA.*;
  39. import com.sun.CORBA.iiop.*;
  40.  
  41. // This is the directory where the idl-to-java compiler put stuff
  42. import PortfolioManager.* ;
  43.  
  44. public
  45. class PortfolioApplet
  46. extends Applet
  47. {
  48.     /** ORB
  49.     **/
  50.     public 
  51.     static org.omg.CORBA.ORB theOrb_;
  52.  
  53.     public 
  54.     static PortfolioManager.Portfolio ref_;
  55.  
  56.     
  57.     private Insets mInsets                 = new Insets(2,2,2,2);
  58.  
  59.     private Label       mPortfolio;
  60.     private Label       mPrice;
  61.     private TextField    mPriceField;
  62.     private Label    mQuantity;
  63.     private TextField   mQuantityField;
  64.     private Label    mTicker;
  65.     private TextField   mTickerField;
  66.     private Button    mAdd;
  67.     private Button      mSell;
  68.     private Button      mBuy;
  69.     private Button      mDelete;
  70.     private Button      mChange;
  71.  
  72.     private Button      mReport;
  73.     private TextArea    mReportText;
  74.  
  75.     public
  76.     void init() {
  77.       setORBConnection();
  78.       super.init();
  79.       createGui();
  80.     }
  81.  
  82.     /** Initialize ORB connection
  83.     **/
  84.     public
  85.     void setORBConnection() {
  86.       try {
  87.         // create and initialize the ORB
  88.         theOrb_ =  org.omg.CORBA.ORB.init(this, null);
  89.         System.out.println("Portfolio Client: Successfull ORB initialization");
  90.  
  91.         // get the root naming context
  92.         org.omg.CORBA.Object objRef =
  93.                 theOrb_.resolve_initial_references("NameService");
  94.         NamingContext ncRef = NamingContextHelper.narrow(objRef);
  95.  
  96.         // resolve the Object Reference in Naming
  97.         NameComponent nc = new NameComponent("Portfolio", "");
  98.         NameComponent path[] = {nc};
  99.         ref_ = PortfolioManager.PortfolioHelper.narrow(ncRef.resolve(path));
  100.  
  101.         System.out.println("Portfolio Client: Portfolio accessed");
  102.       }
  103.       catch (Exception ex) {
  104.         ex.printStackTrace();
  105.         System.out.println("Portfolio Client: Exception-> " + ex) ;
  106.       }
  107.     }
  108.  
  109.     public
  110.     void createGui() {
  111.       setBackground(Color.lightGray);
  112.  
  113.       GridBagLayout gb = new GridBagLayout();
  114.       GridBagConstraints gbc = new GridBagConstraints();
  115.       setLayout(gb);
  116.  
  117.       int row = 0;
  118.  
  119.       gbc.anchor      = GridBagConstraints.NORTH;
  120.       gbc.fill        = GridBagConstraints.BOTH;
  121.       gbc.gridx       = 0;
  122.       gbc.gridy       = row;
  123.       gbc.gridwidth   = 2;
  124.       gbc.gridheight  = 1;
  125.       gbc.weightx     = 1;
  126.       gbc.weighty     = 0;
  127.       mPortfolio = new Label("Portfolio Manager", Label.CENTER);
  128.       mPortfolio.setFont(new Font("Helvetica", Font.BOLD, 16));
  129.       gb.setConstraints(mPortfolio, gbc);
  130.       add(mPortfolio);
  131.  
  132.       row++;
  133.  
  134.       gbc.anchor      = GridBagConstraints.CENTER;
  135.       gbc.fill        = GridBagConstraints.BOTH;
  136.       gbc.gridx       = 0;
  137.       gbc.gridy       = row;
  138.       gbc.gridwidth   = 1;
  139.       gbc.gridheight  = 1;
  140.       gbc.weightx     = 0;
  141.       gbc.weighty     = 0;
  142.       mTicker = new Label("Ticker", Label.LEFT);
  143.       gb.setConstraints(mTicker, gbc);
  144.       add(mTicker);
  145.  
  146.       gbc.anchor      = GridBagConstraints.CENTER;
  147.       gbc.fill        = GridBagConstraints.BOTH;
  148.       gbc.gridx       = 1;
  149.       gbc.gridy       = row;
  150.       gbc.gridwidth   = 1;
  151.       gbc.gridheight  = 1;
  152.       gbc.weightx     = 1;
  153.       gbc.weighty     = 0;
  154.       mTickerField = new TextField("Ticker");
  155.       gb.setConstraints(mTickerField, gbc);
  156.       add(mTickerField);
  157.  
  158.       row++;
  159.  
  160.       gbc.anchor      = GridBagConstraints.CENTER;
  161.       gbc.fill        = GridBagConstraints.BOTH;
  162.       gbc.gridx       = 0;
  163.       gbc.gridy       = row;
  164.       gbc.gridwidth   = 1;
  165.       gbc.gridheight  = 1;
  166.       gbc.weightx     = 0;
  167.       gbc.weighty     = 0;
  168.       mQuantity = new Label("Quantity", Label.LEFT);
  169.       gb.setConstraints(mQuantity, gbc);
  170.       add(mQuantity);
  171.  
  172.       gbc.anchor      = GridBagConstraints.CENTER;
  173.       gbc.fill        = GridBagConstraints.BOTH;
  174.       gbc.gridx       = 1;
  175.       gbc.gridy       = row;
  176.       gbc.gridwidth   = 1;
  177.       gbc.gridheight  = 1;
  178.       gbc.weightx     = 1;
  179.       gbc.weighty     = 0;
  180.       mQuantityField = new TextField("0");
  181.       gb.setConstraints(mQuantityField, gbc);
  182.       add(mQuantityField);
  183.  
  184.       row++;
  185.  
  186.       gbc.anchor      = GridBagConstraints.CENTER;
  187.       gbc.fill        = GridBagConstraints.BOTH;
  188.       gbc.gridx       = 0;
  189.       gbc.gridy       = row;
  190.       gbc.gridwidth   = 1;
  191.       gbc.gridheight  = 1;
  192.       gbc.weightx     = 0;
  193.       gbc.weighty     = 0;
  194.       mPrice = new Label("Price", Label.LEFT);
  195.       gb.setConstraints(mPrice, gbc);
  196.       add(mPrice);
  197.  
  198.       gbc.anchor      = GridBagConstraints.CENTER;
  199.       gbc.fill        = GridBagConstraints.BOTH;
  200.       gbc.gridx       = 1;
  201.       gbc.gridy       = row;
  202.       gbc.gridwidth   = 1;
  203.       gbc.gridheight  = 1;
  204.       gbc.weightx     = 1;
  205.       gbc.weighty     = 0;
  206.       mPriceField = new TextField("0.00");
  207.       gb.setConstraints(mPriceField, gbc);
  208.       add(mPriceField);
  209.  
  210.       row++;
  211.       gbc.anchor      = GridBagConstraints.CENTER;
  212.       gbc.fill        = GridBagConstraints.NONE;
  213.       gbc.gridx       = 0;
  214.       gbc.gridy       = row;
  215.       gbc.gridwidth   = 1;
  216.       gbc.gridheight  = 1;
  217.       gbc.weightx     = 0;
  218.       gbc.weighty     = 0;
  219.       mAdd = new Button("Add");
  220.       gb.setConstraints(mAdd, gbc);
  221.       add(mAdd);
  222.  
  223.       gbc.anchor      = GridBagConstraints.NORTH;
  224.       gbc.fill        = GridBagConstraints.NONE;
  225.       gbc.gridx       = 1;
  226.       gbc.gridy       = row;
  227.       gbc.gridwidth   = 1;
  228.       gbc.gridheight  = 1;
  229.       gbc.weightx     = 0;
  230.       gbc.weighty     = 0;
  231.       mSell = new Button("Sell");
  232.       gb.setConstraints(mSell, gbc);
  233.       add(mSell);
  234.  
  235.       gbc.anchor      = GridBagConstraints.CENTER;
  236.       gbc.fill        = GridBagConstraints.NONE;
  237.       gbc.gridx       = 2;
  238.       gbc.gridy       = row;
  239.       gbc.gridwidth   = 1;
  240.       gbc.gridheight  = 1;
  241.       gbc.weightx     = 0;
  242.       gbc.weighty     = 0;
  243.       mBuy = new Button("Buy");
  244.       gb.setConstraints(mBuy, gbc);
  245.       add(mBuy);
  246.  
  247.       gbc.anchor      = GridBagConstraints.CENTER;
  248.       gbc.fill        = GridBagConstraints.NONE;
  249.       gbc.gridx       = 3;
  250.       gbc.gridy       = row;
  251.       gbc.gridwidth   = 1;
  252.       gbc.gridheight  = 1;
  253.       gbc.weightx     = 0;
  254.       gbc.weighty     = 0;
  255.       mChange = new Button("Change");
  256.       gb.setConstraints(mChange, gbc);
  257.       add(mChange);
  258.  
  259.       gbc.anchor      = GridBagConstraints.CENTER;
  260.       gbc.fill        = GridBagConstraints.NONE;
  261.       gbc.gridx       = 4;
  262.       gbc.gridy       = row;
  263.       gbc.gridwidth   = 1;
  264.       gbc.gridheight  = 1;
  265.       gbc.weightx     = 0;
  266.       gbc.weighty     = 0;
  267.       mDelete = new Button("Delete");
  268.       gb.setConstraints(mDelete, gbc);
  269.       add(mDelete);
  270.  
  271.       row++;
  272.       row++;
  273.  
  274.       gbc.anchor      = GridBagConstraints.CENTER;
  275.       gbc.fill        = GridBagConstraints.NONE;
  276.       gbc.gridx       = 0;
  277.       gbc.gridy       = row;
  278.       gbc.gridwidth   = 1;
  279.       gbc.gridheight  = 1;
  280.       gbc.weightx     = 0;
  281.       gbc.weighty     = 0;
  282.       mReport = new Button("Report");
  283.       gb.setConstraints(mReport, gbc);
  284.       add(mReport);
  285.  
  286.       gbc.anchor      = GridBagConstraints.CENTER;
  287.       gbc.fill        = GridBagConstraints.BOTH;
  288.       gbc.gridx       = 1;
  289.       gbc.gridy       = row;
  290.       gbc.gridwidth   = 4;
  291.       gbc.gridheight  = 1;
  292.       gbc.weightx     = 0;
  293.       gbc.weighty     = 0;
  294.       mReportText = new TextArea(" ", 5, 80);
  295.       gb.setConstraints(mReportText, gbc);
  296.       add(mReportText);
  297.  
  298.  
  299.    }
  300.  
  301.    /**
  302.     *  Handle action events from various controls
  303.     *  Overrides Component.action().
  304.     */
  305.    public
  306.    boolean action(Event pe, java.lang.Object po) {
  307.      // User selected month from the choice
  308.      try {
  309.        if (pe.target == mAdd) {
  310.          getAppletContext().showStatus("Add " + mTickerField.getText() 
  311.            + " to Portfolio at US$" + mPriceField.getText());
  312.      Double price_ = Double.valueOf(mPriceField.getText());
  313.      java.lang.Integer quantity_ = 
  314.           java.lang.Integer.valueOf(mQuantityField.getText());
  315.          ref_.addStock(mTickerField.getText(), 
  316.                (short)quantity_.intValue(),
  317.                        price_.doubleValue());
  318.      updateReport();
  319.          return true;
  320.        }
  321.        if (pe.target == mSell) {
  322.          getAppletContext().showStatus("Sell " + mQuantityField.getText() 
  323.            + " shares of " + mTickerField.getText() + " at price US$ " 
  324.            + mPriceField.getText());
  325.      java.lang.Integer quantity_ = 
  326.           java.lang.Integer.valueOf(mQuantityField.getText());
  327.      ref_.sell(mTickerField.getText(), (short)quantity_.intValue());
  328.      updateReport();
  329.          return true;
  330.        }
  331.        if (pe.target == mBuy) {
  332.          getAppletContext().showStatus("Buy " + mQuantityField.getText() 
  333.            + " shares of " + mTickerField.getText() + " at price US$ " 
  334.            + mPriceField.getText());
  335.      java.lang.Integer quantity_ = 
  336.           java.lang.Integer.valueOf(mQuantityField.getText());
  337.      ref_.buy(mTickerField.getText(), (short)quantity_.intValue());
  338.      updateReport();
  339.          return true;
  340.        }
  341.        if (pe.target == mDelete) {
  342.          getAppletContext().showStatus("Delete " + mTickerField.getText() 
  343.            + " from Portfolio");
  344.      ref_.deleteStock(mTickerField.getText());
  345.      updateReport();
  346.          return true;
  347.        }
  348.        if (pe.target == mChange) {
  349.          getAppletContext().showStatus("Change " + mTickerField.getText() 
  350.            + " in Portfolio to US$" + mPriceField.getText());
  351.      Double price_ = Double.valueOf(mPriceField.getText());
  352.      java.lang.Integer quantity_ = 
  353.           java.lang.Integer.valueOf(mQuantityField.getText());
  354.      ref_.deleteStock(mTickerField.getText());
  355.          ref_.addStock(mTickerField.getText(), 
  356.                (short)quantity_.intValue(),
  357.                        price_.doubleValue());
  358.      updateReport();
  359.          return true;
  360.        }
  361.        if (pe.target == mReport) {
  362.      updateReport();
  363.      return true;
  364.        }
  365.        return(super.action(pe, po));
  366.      }
  367.      catch(NumberFormatException ex) {
  368.        System.out.println("Exception: " + ex);
  369.      }
  370.      catch(Exception ex) {
  371.        ex.printStackTrace();
  372.        System.out.println("Exception: " + ex);
  373.      }
  374.      return true;
  375.    }
  376.  
  377.    void updateReport() {
  378.      mReportText.replaceText(ref_.report(), 0,
  379.              mReportText.getText().length());
  380.      mReportText.appendText("\n-----------------------------\n");
  381.      mReportText.appendText("Total Value: US $" + new Double(ref_.value()) + "\n");
  382.  
  383.    }
  384.  
  385.  
  386.     /**
  387.      *  Paint nice raized 3D background.
  388.      *  Overrides Component.paint().
  389.      */
  390.     public
  391.     void
  392.     paint(Graphics pg)
  393.     {
  394.         pg.setColor(Color.black);
  395.         pg.drawLine(1, 0, bounds().width-2, 0);
  396.         pg.drawLine(0, 1, 0, bounds().height-2);
  397.         pg.drawLine(1, bounds().height-1, bounds().width - 2, bounds().height - 1);
  398.         pg.drawLine(bounds().width - 1, 1, bounds().width - 1, bounds().height - 2);
  399.  
  400.         pg.setColor(getBackground());
  401.         //pg.draw3DRect(1, 1, bounds().width - 3, bounds().height - 3, true);
  402.         pg.fill3DRect(1, 1, bounds().width - 2, bounds().height - 2, true);
  403.     }
  404.  
  405.     /**
  406.      *  For better looks, let Layout Manager leave some margin.
  407.      */
  408.     public
  409.     Insets
  410.     insets()
  411.     {
  412.         return mInsets;
  413.     }
  414. }
  415.